package com.investment.advisor.controller;

import com.investment.advisor.dto.FactorDTO;
import com.investment.advisor.service.FactorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/factors")
@Api(tags = "因子管理", description = "因子的增删改查")
public class FactorController {

    private final FactorService factorService;

    @Autowired
    public FactorController(FactorService factorService) {
        this.factorService = factorService;
    }

    @GetMapping
    @ApiOperation("获取所有因子")
    public ResponseEntity<List<FactorDTO>> getAllFactors() {
        return ResponseEntity.ok(factorService.getAllFactors());
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取因子")
    public ResponseEntity<FactorDTO> getFactorById(
            @ApiParam(value = "因子ID", required = true)
            @PathVariable Long id) {
        return ResponseEntity.ok(factorService.getFactorById(id));
    }

    @GetMapping("/category/{category}")
    @ApiOperation("根据分类获取因子")
    public ResponseEntity<List<FactorDTO>> getFactorsByCategory(
            @ApiParam(value = "因子分类", required = true)
            @PathVariable String category) {
        return ResponseEntity.ok(factorService.getFactorsByCategory(category));
    }

    @GetMapping("/search")
    @ApiOperation("搜索因子")
    public ResponseEntity<List<FactorDTO>> searchFactors(
            @ApiParam(value = "搜索关键词", required = true)
            @RequestParam String keyword) {
        return ResponseEntity.ok(factorService.searchFactors(keyword));
    }

    @PostMapping
    @ApiOperation("创建因子")
    public ResponseEntity<FactorDTO> createFactor(
            @ApiParam(value = "因子信息", required = true)
            @RequestBody FactorDTO factorDTO) {
        return new ResponseEntity<>(factorService.createFactor(factorDTO), HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新因子")
    public ResponseEntity<FactorDTO> updateFactor(
            @ApiParam(value = "因子ID", required = true)
            @PathVariable Long id,
            @ApiParam(value = "因子信息", required = true)
            @RequestBody FactorDTO factorDTO) {
        return ResponseEntity.ok(factorService.updateFactor(id, factorDTO));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除因子")
    public ResponseEntity<Void> deleteFactor(
            @ApiParam(value = "因子ID", required = true)
            @PathVariable Long id) {
        factorService.deleteFactor(id);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/test")
    @ApiOperation("测试因子")
    public ResponseEntity<Map<String, Object>> testFactor(
            @ApiParam(value = "因子信息", required = true)
            @RequestBody FactorDTO factorDTO) {
        // 这里应该实现因子测试逻辑
        // 暂时返回模拟数据
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        resultMap.put("coverage", 0.985);
        resultMap.put("ic", 0.156);
        resultMap.put("ir", 0.823);
        resultMap.put("computeTime", 2.5);
        return ResponseEntity.ok(resultMap);
    }
} 